package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.AdvposPageQueryDTO;
import com.ydd.entity.YddAdvpos;
import com.ydd.exception.BaseException;
import com.ydd.mapper.YddAdvimgMapper;
import com.ydd.mapper.YddAdvposMapper;
import com.ydd.result.PageResult;
import com.ydd.service.YddAdvposService;
import com.ydd.vo.AdvposVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:29
 */
@Service
public class YddAdvposServiceImpl implements YddAdvposService {
    @Autowired
    private YddAdvposMapper yddAdvposMapper;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    /**
     * 广告位分页查询
     *
     * @param advposPageQueryDTO
     * @return
     */

    @Override
    public PageResult page(AdvposPageQueryDTO advposPageQueryDTO) {

        Integer page = advposPageQueryDTO.getPage();
        Integer limit = advposPageQueryDTO.getLimit();
        //设置分页数据
        PageHelper.startPage(page, limit);
        //调用方法
        List<AdvposVO> list = yddAdvposMapper.page(advposPageQueryDTO);


        //获取广告位下的广告图数量，封装成vo对象返回
        for (AdvposVO advposVO : list) {
            Integer count = yddAdvimgMapper.count(advposVO.getId());
            advposVO.setAdvposnum(count);
        }
        //转换成Page类型
        Page<AdvposVO> yddAdvpos = (Page<AdvposVO>) list;


        return new PageResult(yddAdvpos.getTotal(),
                yddAdvpos.getPageNum(),
                yddAdvpos.getPageSize(),
                yddAdvpos.getPages(),
                list);
    }

    /**
     * 广告位全部查询
     *
     * @return
     */
    @Override
    public List<YddAdvpos> all() {
        List<YddAdvpos> list = yddAdvposMapper.selectList(null);

        return list;
    }

    /**
     * 根据ID查询广告位详情
     *
     * @param id
     * @return
     */
    @Override
    public YddAdvpos selectById(String id) {
        YddAdvpos yddAdvpos = yddAdvposMapper.selectById(id);
        return yddAdvpos;
    }

    /**
     * 广告位修改
     *
     * @param yddAdvpos
     */
    @Override
    public void update(YddAdvpos yddAdvpos) {
        yddAdvposMapper.updateById(yddAdvpos);
    }


    /**
     * 广告位添加
     *
     * @param yddAdvpos
     */
    @Override
    public void add(YddAdvpos yddAdvpos) {
        yddAdvpos.setDefaultData("1");
        yddAdvposMapper.insert(yddAdvpos);
    }

    /**
     * @param id
     */
    @Override
    public void delete(String id) {
        Integer count = yddAdvimgMapper.count(Long.valueOf(id));
        if (count>0){
            throw new BaseException("当前广告位下有广告图，不能删除");
        }else {
            yddAdvposMapper.deleteById(id);}
    }
}
